<html>
<head><meta charset="utf-8"><title>borrow not used anymore but still in scope · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow.20not.20used.20anymore.20but.20still.20in.20scope.html">borrow not used anymore but still in scope</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="199873762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow%20not%20used%20anymore%20but%20still%20in%20scope/near/199873762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow.20not.20used.20anymore.20but.20still.20in.20scope.html#199873762">(Jun 05 2020 at 13:14)</a>:</h4>
<p>If I have unsafe code manipulating <code>UnsafeCell&lt;T&gt;</code>, I must ensure that <code>&amp;T</code> and <code>&amp;mut T</code> to the same value do not "exist" at the same time. Does "exist" include a local variable that is not used anymore but still in scope? <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7e4ceb12450ebdf13d05497562e79778">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=7e4ceb12450ebdf13d05497562e79778</a></p>



<a name="199891667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow%20not%20used%20anymore%20but%20still%20in%20scope/near/199891667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow.20not.20used.20anymore.20but.20still.20in.20scope.html#199891667">(Jun 05 2020 at 15:18)</a>:</h4>
<p>I don't think so. According to NLL, the scope of a variable ends when it is last used.</p>



<a name="199894347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow%20not%20used%20anymore%20but%20still%20in%20scope/near/199894347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow.20not.20used.20anymore.20but.20still.20in.20scope.html#199894347">(Jun 05 2020 at 15:37)</a>:</h4>
<p>OTOH, at least under current rules, we do mark it as <code>dereferenceable</code> so you shouldn't free the memory behind it</p>



<a name="199894387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow%20not%20used%20anymore%20but%20still%20in%20scope/near/199894387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow.20not.20used.20anymore.20but.20still.20in.20scope.html#199894387">(Jun 05 2020 at 15:37)</a>:</h4>
<p>though how that plays into aliasing UB I'm not sure</p>



<a name="199894722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow%20not%20used%20anymore%20but%20still%20in%20scope/near/199894722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow.20not.20used.20anymore.20but.20still.20in.20scope.html#199894722">(Jun 05 2020 at 15:40)</a>:</h4>
<p>Good point Amanieu, NLL allows <code>let mut x = something; let a = &amp;mut x; let b = &amp;mut x;</code> with <code>a</code> still in lexical scope when <code>b</code> is introduced</p>



<a name="200515920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow%20not%20used%20anymore%20but%20still%20in%20scope/near/200515920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow.20not.20used.20anymore.20but.20still.20in.20scope.html#200515920">(Jun 11 2020 at 08:13)</a>:</h4>
<p><span class="user-mention" data-user-id="219747">@Simon Sapin</span> Stacked Borrows says that "exist" lasts until the last "use" of a reference, where "use" includes reborrows and dereferencing (so any [implicit] use of the <code>*</code> operator, not just actual memory accesses). <em>Additionally</em>, if your reference is passed as an argument to a function, then the reference "exists" until the function returns.</p>



<a name="200515968"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow%20not%20used%20anymore%20but%20still%20in%20scope/near/200515968" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/borrow.20not.20used.20anymore.20but.20still.20in.20scope.html#200515968">(Jun 11 2020 at 08:13)</a>:</h4>
<p>(That last clause is related to <code>dereferencable</code> meaning "dereferencable during the entire function this was passed to".)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>